package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.boot.pojos.User;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 作为生产者发送消息的控制层
 */
@RestController
@RequestMapping("/api/v1/hello")
public class HelloController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息的方法
     * @return
     */
    @GetMapping
    public String sendMessage(){
        for (int i = 1; i <=5; i++) {
            if(i%2==0){
                kafkaTemplate.send("boot_stream_topic", "hello itcast");
            }else{
                kafkaTemplate.send("boot_stream_topic", "hello kafka");
            }
        }
        return "发送成功";
    }
}
